package com.sinaapp.kltq.weathermanager;

import android.content.Context;

import com.sinaapp.kltq.data.CityListItem;
import com.sinaapp.kltq.data.WeatherAlarm;
import com.sinaapp.kltq.data.WeatherDay;
import com.sinaapp.kltq.data.WeatherEnvironment;
import com.sinaapp.kltq.data.WeatherRealTime;
import com.sinaapp.kltq.data.WeatherZhishu;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * Created by 丰小兄 on 2015/7/16.
 * Function: 函数接口层（共用模块）
 */
public class WeatherManager {
    public WeatherManager(Context _context) {
        context = _context;
        weatherSqlite = new WeatherSqlite(context);
    }

    // City management
    public boolean addCityIntoListEnd(String city) {
        return weatherSqlite.addCityIntoListEnd(city);
    }
    public boolean delCityFromList(String city) {
        return weatherSqlite.delCityFromList(city);
    }
    public boolean dragCityUpon(String city1, String city2) {
        return weatherSqlite.dragCityUpon(city1, city2);
    }
    public List<CityListItem> getCityList() { // size 0 means failure
        List<CityListItem> list = weatherSqlite.getCityList();
        Collections.sort(list, new Comparator<CityListItem>() {
            @Override
            public int compare(CityListItem lhs, CityListItem rhs) {
                return (lhs.cityIndex > rhs.cityIndex) ? 1 : -1;
            }
        });
        return list;
    }
    public boolean checkCity(String city) { // has network
        return weatherSqlite.checkCity(city, context);
    }
    // Update management
    public boolean pleaseUpdateSir() {    // has network
        return weatherSqlite.updateAllCity(context);
    }
    public boolean pleaseUpdateThisSir(String city) {   // has network
        return weatherSqlite.updateCity(city, context);
    }
    public Date getUpdateDate(String city) {
        return weatherSqlite.getUpdateDate(city);
    }
    // Real time weather data  management, null means failure
    public WeatherRealTime getRealTimeWeather(String city) {
        return weatherSqlite.getRealTimeWeather(city);
    }
    // Environment weather data management, null means failure
    public WeatherEnvironment getEnvironmentWeather(String city) {
        return weatherSqlite.getEnvironmentWeather(city);
    }
    // Forecast weather data management, null means failure
    public List<WeatherDay> getForecastWeather(String city) {
        return weatherSqlite.getForecastWeather(city);
    }
    // Zhishu weather data management, null means failure
    public List<WeatherZhishu> getZhishuWeather(String city) {
        return weatherSqlite.getZhishuWeather(city);
    }
    // Alarm weather data management, null means failure
    public WeatherAlarm getAlarmWeather(String city) {
        return weatherSqlite.getAlarmWeather(city);
    }

    private WeatherSqlite weatherSqlite;
    private Context context;
}
